package qlc;

public class GenericSort {
    public static void main(String[] args) {
        Integer[] intArray = {2, 3, 4};
        Double[] doubleArray = {3.4, 1.3, -22.1};
        Character[] characterArray = {'a', 'J', 'r'};
        String[] stringArray = {"Tom", "Sosan", "Kim"};
        sort(intArray);
        sort(doubleArray);
        sort(characterArray);
        sort(stringArray);
        System.out.print("Sorted Integer objects: ");
        printList(intArray);
        System.out.print("Sorted Double objects: ");
        printList(doubleArray);
        System.out.print("Sorted Character objects: ");
        printList(characterArray);
        System.out.print("Sorted String objects: ");
        printList(stringArray);
    }

    public static <E extends Comparable<E>> void sort(E[] list) {
        E currentMin ;
        int currentMinIndex;
        for (int i = 0; i < list.length - 1; i++) {
            currentMin = list[i];
            currentMinIndex = i;
            for (int j = i + 1; j < list.length; j++) {
                if(currentMin.compareTo(list[j])>0){
                currentMin = list[j];
                currentMinIndex = j;
            }
        }
        if (currentMinIndex != i) {
            list[currentMinIndex] = list[i];
            list[i] = currentMin;
        }
    }
}
    public static void printList(Object[] list) {
        for (int i=0;i<list.length; i++){
            System.out.println(list[i]+" ");
        }
        System.out.println();
    }
}
